/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tbquestion_client.form;

import fsktms.request.IRequestDelegate;
import fsktms.request.Request;
import fsktms.request.RequestParameters;
import fsktms.request.RequestRespond;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import model.ClientModel;
import qclient.QClient;

/**
 *
 * @author salm
 */
public class ChangePassword extends javax.swing.JPanel {

    JDialog parent;
    /**
     * Creates new form ChangePassword
     */
    public ChangePassword(JDialog parent) {
        initComponents();
        
        this.parent = parent;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPasswordField2 = new javax.swing.JPasswordField();
        jPasswordField3 = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        pwfOld = new javax.swing.JPasswordField();
        pwfNew = new javax.swing.JPasswordField();
        pwfConfirm = new javax.swing.JPasswordField();
        btnChangePassword = new javax.swing.JButton();

        jPasswordField2.setText("jPasswordField2");

        jPasswordField3.setText("jPasswordField3");

        jLabel1.setText("Old Password");

        jLabel2.setText("New Password");

        jLabel3.setText("Confirm");

        btnChangePassword.setText("Change Password");
        btnChangePassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangePasswordActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnChangePassword)
                        .addGap(0, 64, Short.MAX_VALUE))
                    .addComponent(pwfOld)
                    .addComponent(pwfNew)
                    .addComponent(pwfConfirm, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(pwfOld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(pwfNew, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(pwfConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addComponent(btnChangePassword)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnChangePasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangePasswordActionPerformed
        // TODO add your handling code here:
        String oldPassword = String.valueOf( this.pwfOld.getPassword() );
        String newPassword = String.valueOf( this.pwfNew.getPassword() );
        String confirm = String.valueOf( this.pwfConfirm.getPassword() );
        
        
        if(makeSureAllFields() )
        {
            return;
        }
        try 
        {
            RequestParameters params = new RequestParameters()
                                            .set(RequestParameters.KEY_FUNCTION, "changeAccountPassword")
                                            .set("ID", ClientModel.getInstance().getAccount().getUserID())
                                            .set("OldPassword", oldPassword)
                                            .set("NewPassword", newPassword);
            final ChangePassword copyThis = this;
            QClient.getInstance().getRequestManager().sendRequest(
                    new Request("Account", params, new IRequestDelegate() {

                @Override
                public void requestCompleted(RequestRespond respond) {
                    if (respond.isSuccess() && (boolean) respond.getResult())
                    {
                        JOptionPane.showMessageDialog(copyThis, "Password changed");
                        parent.dispose();
                    }
                    else
                    {
                        // NEED TABLE
                        JOptionPane.showMessageDialog(copyThis, "Old password is incorrect");
                    }
                }
            }));
        } catch (IOException ex)
        {
            Logger.getLogger(loginForm.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(this, ex);
        }
    }//GEN-LAST:event_btnChangePasswordActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChangePassword;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField jPasswordField2;
    private javax.swing.JPasswordField jPasswordField3;
    private javax.swing.JPasswordField pwfConfirm;
    private javax.swing.JPasswordField pwfNew;
    private javax.swing.JPasswordField pwfOld;
    // End of variables declaration//GEN-END:variables
    
    
    private boolean makeSureAllFields()
    {
        //NEED TABLE
        String oldPassword = String.valueOf( this.pwfOld.getPassword() );
        String newPassword = String.valueOf( this.pwfNew.getPassword() );
        String confirm = String.valueOf( this.pwfConfirm.getPassword() );
        if("".equals(oldPassword) || "".equals(newPassword))
        {
            JOptionPane.showMessageDialog( this, "You must enter all fields" );
            return true;
        }
        
        if(!newPassword.equals(confirm))
        {
            JOptionPane.showMessageDialog( this, "Confirm password must correct with new password" );
            return true;
        }
        
        return false;
    }
}
